package com.spider.admin.store.primary.mapper;

import com.spider.admin.store.primary.model.ClientInfo;
import jakarta.validation.constraints.NotNull;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ClientMapper {

    @Insert("INSERT INTO t_client_info(pid,client_name,client_key,tenant_id,grant_date,end_date,client_desc,archive_status,create_time,update_time)" +
            " values(#{pid},#{clientName},#{clientKey},#{tenantId},#{grantDate},#{endDate},#{clientDesc},#{archiveStatus},NOW(),NOW()) ")
    int saveClient(ClientInfo clientInfo);

    @Select("select * from t_client_info where pid=#{pid}")
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "client_name", property = "clientName"),
            @Result(column = "client_key", property = "clientKey"),
            @Result(column = "tenant_id", property = "tenantId"),
            @Result(column = "grant_date", property = "grantDate"),
            @Result(column = "end_date", property = "endDate"),
            @Result(column = "client_desc", property = "clientDesc"),
            @Result(column = "archive_status", property = "archiveStatus")
    })
    ClientInfo getClientById(String pid);

    @Update({"<script>",
            "Update t_client_info ",
            "<set>",
            " update_time=now() ",
            "<if test='clientInfo.clientName != null'> ,client_name = #{clientInfo.clientName}</if>",
            "<if test='clientInfo.clientKey != null'> ,client_key = #{clientInfo.clientKey}</if>",
            "<if test='clientInfo.endDate != null'> ,end_date = #{clientInfo.endDate}</if>",
            "<if test='clientInfo.clientDesc != null'> ,client_desc = #{clientInfo.clientDesc}</if>",
            "<if test='clientInfo.archiveStatus != null'> ,archive_status = #{clientInfo.archiveStatus}</if>",
            "</set>",
            " WHERE pid=#{pid}",
            "</script>"})
    int updateClient(ClientInfo clientInfo, String pid);

    @Select({
            "Select count(0) ct from t_client_info where pid=#{clientId} and archive_status='active'"})
    long getActiveClientCountById(@NotNull String clientId);

    @Select({"<script>",
            "Select count(0) ct from t_client_info where 1=1 ",
            "<if test='clientName != null'> and client_name = #{clientName}</if>",
            "<if test='clientId != null'> and pid != #{clientId}</if>",
            "</script>"})
    long getClientCountUpdateName(String clientId, String clientName);


    @Select({"<script>",
            "Select count(0) from t_client_info WHERE tenant_id=#{tenantId} ",
            "<if test='clientName != null'> AND client_name = #{clientName}</if>",
            "<if test='clientStatus != null'> AND archive_status = #{clientStatus}</if>",
            "</script>"})
    long getClientRecordNum(String tenantId, String clientName, String clientStatus);

    @Select({"<script>",
            "Select * from t_client_info WHERE tenant_id=#{tenantId} ",
            "<if test='clientName != null'> AND client_name = #{clientName}</if>",
            "<if test='clientStatus != null'> AND archive_status = #{clientStatus}</if>",
            " limit #{pageSize} offset #{startRow}",
            "</script>"})
    @Results(value = {
            @Result(column = "pid", property = "pid"),
            @Result(column = "client_name", property = "clientName"),
            @Result(column = "client_key", property = "clientKey"),
            @Result(column = "tenant_id", property = "tenantId"),
            @Result(column = "grant_date", property = "grantDate"),
            @Result(column = "end_date", property = "endDate"),
            @Result(column = "client_desc", property = "clientDesc"),
            @Result(column = "archive_status", property = "archiveStatus")
    })
    List<ClientInfo> queryClientByTenantId(int startRow, Integer pageSize, String tenantId, String clientName, String clientStatus);

    @Delete("delete from t_client_info where pid=#{clientId}")
    int removeClient(String clientId);
}
